/**
 *
 * © Bahadir Zeren 2011
 * bzeren@gmail.com
 * No warranties, express or implied, are made for this program.
 *
 */
package org.baatar.util;

import java.io.File;
import java.io.FileInputStream;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.helpers.DefaultHandler;

/**
 * 
 * 
 * @author B_ZEREN
 */
public class XMLReader {

    public static boolean readXML(String fileName,
                                    DefaultHandler xmlHandler) throws Exception {
        File file = new File(fileName);
        return readXML(file, xmlHandler);
    }

    public static boolean readXML(File file,
                                    DefaultHandler xmlHandler) throws Exception {
        FileInputStream fis = new FileInputStream(file);
        try {
            return readXML(fis, xmlHandler);
        } finally {
            fis.close();
        }
    }

    public static boolean readXML(FileInputStream fileInputStream,
                                    DefaultHandler xmlHandler) throws Exception {
        boolean res = false;
        try {
            SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
            SAXParser saxParser = saxParserFactory.newSAXParser();
            saxParser.parse(fileInputStream, xmlHandler);
            res = true;
        } catch (Exception ex) {
            ex.printStackTrace();
            throw ex;
        }
        return res;
    }
}